import Vue from 'vue'
import VueRouter from 'vue-router'
import layout from './modules/layout'
import pageNoFind from './modules/pageNoFind'
import login from './modules/login'
import { Toast } from 'vant'

Vue.use(VueRouter)
const routes = [
  ...layout,
  ...login,
  ...pageNoFind

]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (!to.meta.auth && !localStorage.token) {
    Toast('请先去登录,正在自动跳转')
    setTimeout(() => {
      next('/login')
    }, 1500)
  } else {
    next()
  }
})

router.afterEach((to, from) => {
  if (to.path === '/home' && from.path === '/login') {
    location.reload()
  }
})
export default router
